'use strict';
app.controller('userCtrl',['$scope','utilService','$modal','userService','toaster','$state', function($scope,utilService,$modal,userService,toaster,$state){
    $scope.user = {};
    $scope.userVipVO={
        isPresent:0
    };

    $scope.identityObject = [
        //{name: '普通用户', value: '0'},
        {name: 'VIP用户', value: '1'},
        {name: '渠道合伙人', value: '2'}
    ]

    // call server.
    $scope.query = function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'user',
            params:$scope.user
        }).then(function(result){
            console.log(result);
        });
    }
    $scope.query();

    $scope.getVipName= function(){
        userService.getVipName($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.vipVO=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }
    $scope.getVipName();

    //设置用户为渠道合伙人
    $scope.nameUnchange=1;
    $scope.mobilePhoneUnchange=1;
    $scope.updateUserIdentity=function(id,name,mobilePhone,vipId,vipIdentity){
        if(angular.isUndefined(name)||name==""||name==null){
            $scope.nameUnchange=0;
        }
        if(angular.isUndefined(mobilePhone)||mobilePhone==""||mobilePhone==null){
            $scope.mobilePhoneUnchange=0;
        }
        $scope.userVipVO.userId=id;
        $scope.userVipVO.vipId=vipId;
        $scope.userVipVO.name=name;
        $scope.userVipVO.mobilePhone=mobilePhone;
        $scope.userVipVO.vipIdentity=vipIdentity;
        var modalInstance = $modal.open({
            templateUrl: 'tpl/user/user_vip_popup.html',
            controller: 'userVipPopupCtrl',
            size: 'md',
            scope:$scope
        });
        modalInstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        }, function () {
            console.info('Modal dismissed at: ' + new Date());
        });
    }
}]);
app.controller('userVipPopupCtrl', ['$scope', '$modalInstance','userService','toaster','utilService', function($scope, $modalInstance,userService,toaster,utilService){
    $scope.AllowToReport=true;
    $scope.ok=function(){
        if(!$scope.AllowToReport){
            return;
        }
        $scope.AllowToReport=false;
        userService.updateUserIdentity($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $modalInstance.dismiss('success');
                    toaster.pop('success', "提示", "操作成功");
                    $scope.AllowToReport=true;
                    $scope.query();
                    break;
                case 700:
                    $modalInstance.dismiss('error');
                    toaster.pop('error', "操作错误", "该用户已经设置该身份，请勿重复设置！");
                    $scope.AllowToReport=true;
                    $scope.query();
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
        $modalInstance.dismiss('success');
    }
    //取消弹窗
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
}]);
